<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Foto extends CI_Controller {

    function __construct() {
        parent::__construct();

        /* Standard Libraries of codeigniter are required */
        $this->load->database();
        $this->load->helper('url');
        /* ------------------ */
        $this->load->model('Model_Foto');
        $this->load->model('Model_Tokoh');
        $this->load->model('Model_Kegiatan');
        $this->load->library('ExCRUD');
    }

    function sambutan_kepsek() {
        $crud = new ExCRUD();
        $crud->standartCheck();
        $data['listFoto'] = $this->Model_Foto->getFoto('STATIC_CONTENT','5');
        $this->load->view('admin/sambutan_kepsek', $data);
    }

    function sejarah() {
        $crud = new ExCRUD();
        $crud->standartCheck();
        $data['listFoto'] = $this->Model_Foto->getFoto('STATIC_CONTENT','6');
        $this->load->view('admin/sejarah', $data);
    }

    function tokoh() {
        $crud = new ExCRUD();
        $crud->standartCheck();
        $data['listTokoh'] = $this->Model_Tokoh->getTokoh();
        $this->load->view('admin/tokoh', $data);
    }

    function tokoh_detil($id_tokoh) {
        $crud = new ExCRUD();
        $crud->standartCheck();
        $data['listFoto'] = $this->Model_Foto->getFoto('tokoh',$id_tokoh);
        $data['id'] = $id_tokoh;
        $this->load->view('admin/tokoh_detil', $data);
    }

    function kegiatan() {
        $crud = new ExCRUD();
        $crud->standartCheck();
        $data['listKegiatan'] = $this->Model_Kegiatan->getKegiatan();
        $this->load->view('admin/kegiatan', $data);
    }

    function kegiatan_detil($id_kegiatan) {
        $crud = new ExCRUD();
        $crud->standartCheck();
        $data['listFoto'] = $this->Model_Foto->getFoto('kegiatan',$id_kegiatan);
        $data['id'] = $id_kegiatan;
        $this->load->view('admin/kegiatan_detil', $data);
    }

    function tambah($ref_menu, $ref_content) {
        $this->Model_Foto->tambahFoto($ref_menu, $ref_content, $this->input->post('title'));
        $this->reloadPage($ref_menu, $ref_content);
    }

    function hapus($ref_menu, $ref_content, $id_foto) {
        $this->Model_Foto->hapusFoto($id_foto);
        $this->reloadPage($ref_menu, $ref_content);
    }

    function uploader($field_name, $id_foto, $ref_menu, $ref_content) {
        $config = array();
        $config['upload_path'] = getcwd()."\\foto";
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size']	= '0';
        $pathName = $ref_menu."_".$ref_content."_".$id_foto;
        $config['file_name'] = $pathName;
        $config['overwrite'] = true;

        $this->load->library('upload', $config);

        if (!$this->upload->do_upload($field_name)) {
            $data['error'] = $this->upload->display_errors();
            if (strpos($data['error'], 'file to upload') > 0) {
                $data['error'] = 'Title berhasil diganti';
            }
            $this->Model_Foto->updatePathFoto($id_foto, '',$this->input->post('title'));
            $this->reloadPage($ref_menu, $ref_content);
        }
        else {
            $data = $this->upload->data();
            $this->Model_Foto->updatePathFoto($id_foto, $pathName.$data['file_ext'],$this->input->post('title'));
            $data['success'] = "Foto ".$data['client_name']." berhasil diupload.";
            $this->reloadPage($ref_menu, $ref_content);
        }
    }

    function reloadPage($ref_menu, $ref_content) {
        $data['listFoto'] = $this->Model_Foto->getFoto($ref_menu,$ref_content);
        $data['id'] = $ref_content;
        $this->load->view($this->input->post('page_sender'), $data);

    }
}



/* End of file main.php */
/* Location: ./application/controllers/main.php */